package org.fude.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.fude.dao.UserMapper;
import org.fude.entity.User;
import org.fude.result.Result;
import org.fude.result.ResultCode;
import org.fude.util.StringBufferUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
public class UserController {

	@Value("${fude.page.head.path}")
	private String url;
	
	@Autowired
    private HttpSession session;
	
	@Autowired
	UserMapper userMapper;
	
	@PostMapping("user/login")
	HttpEntity<Result> ts(@Validated User oUser, BindingResult bindingResult) throws Exception{
		if(bindingResult.hasFieldErrors()) {
			FieldError error = bindingResult.getFieldError();
			
			return new ResponseEntity<Result>(
					Result.failure(ResultCode.USER_NOT_EXIST).setMsg(error.getDefaultMessage()), 
					HttpStatus.UNAUTHORIZED);
		}
		
		String name = oUser.getUname();
		String password = oUser.getPwd();
		User user = userMapper.findByUname(name);
		if(user == null) {
			return new ResponseEntity<Result>(
					Result.failure(ResultCode.USER_NOT_EXIST), 
					HttpStatus.UNAUTHORIZED);
		}
		
		if(user.getStatus() != 1) {
			return new ResponseEntity<Result>(
					Result.failure(ResultCode.USER_ACCOUNT_FORBIDDEN),
					HttpStatus.UNAUTHORIZED);
		}
		
		if(!user.getPwd().equals(password)) {
			return new ResponseEntity<Result>(
					Result.failure(ResultCode.USER_PWD_ERROR),
					HttpStatus.UNAUTHORIZED);
					
		}
		session.setAttribute("user", user);
		JSONObject result = new JSONObject();
		result.put("url", url);
		
		return new ResponseEntity<Result>(
				Result.success(result),
				HttpStatus.OK);

	}
	@GetMapping("user/parent")
	User userSelectList() throws Exception{
		
		User user = getUser();
		return user;
	}
	@PostMapping("user")
	boolean user(User user) throws Exception{
		
		User nUser = getUser();
		user.setParent(nUser.getId());
		int order = nUser.getUorder() + 1;
		user.setUorder(order);
		
		if(nUser.getUorder() == 0) {
			user.setParents(String.valueOf(nUser.getId()));
		}else {
			String[] values = new String[] {nUser.getParents(),",",String.valueOf(nUser.getId())};
			user.setParents(StringBufferUtil.append(values));
		}
		System.out.println(user);
		userMapper.addUser(user);
		return true;
	}
	@GetMapping("user/list")
	List<User> userList(int page, int pageSize) throws Exception{
		//if()
		User nUser = getUser();
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("id", nUser.getId());
		map.put("page", (page - 1) * pageSize );
		map.put("pageSize", pageSize);
		List<User> list = userMapper.getChildrenUserListById(map);
		return list;
	}
	@GetMapping("user/detail/list")
	List<User> userDetailList(int page, int pageSize) throws Exception{
		User nUser = getUser();
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("id", nUser.getId());
		map.put("page", (page - 1) * pageSize );
		map.put("pageSize", pageSize);
		List<User> list = userMapper.getDetailChildrenUserListById(map);
		return list;
	}
	
	
	private User getUser() {
		User nUser = (User) session.getAttribute("user");
		return nUser;
	}
}
